#include "grate_test_utils.h"
#include "grate/util/ext_rational.h"

#include "grate/util/common.h"

using growth_rate::ExtendedRational;

namespace {

TEST(ExtendedRationalTest, IntegerTest) {
  ExtendedRational two(2);
  ExtendedRational two_plus(2, 1, true);
  ExtendedRational three(3);
  ExtendedRational six(6);
  EXPECT_LT(two.compare(two_plus), 0);
  EXPECT_EQ(0, two.compare(two));
  EXPECT_GT(two_plus.compare(two), 0);
  EXPECT_LT(two_plus.compare(three), 0);
  EXPECT_EQ(0, six.compare(two.multiply(three)));
  EXPECT_EQ(2U, two.int_value());
  EXPECT_EQ(6U, two.multiply(three).int_value());
}

TEST(ExtendedRationalTest, FractionalTest) {
  ExtendedRational zero;
  ExtendedRational n3d2p(3, 2, true);
  ExtendedRational n7d6p(7, 6, true);
  ExtendedRational n7d4(7, 4, false);
  ExtendedRational n7d4p(7, 4, true);
  EXPECT_GT(n3d2p.compare(n7d6p), 0);
  EXPECT_GT(n7d6p.compare(zero), 0);
  EXPECT_EQ(1U, n3d2p.int_value());
  ExtendedRational prod = n3d2p.multiply(n7d6p);  // should be 7/4+
  EXPECT_EQ(1U, prod.int_value());
  EXPECT_LT(n7d4.compare(prod), 0);
  EXPECT_EQ(0, n7d4p.compare(prod));
  EXPECT_LT(prod.compare(ExtendedRational(2)), 0);
}

TEST(ExtendedRationalTest, DumpTest) {
  ExtendedRational zero;
  EXPECT_EQ("0", zero.dump());
  ExtendedRational three(3);
  EXPECT_EQ("3", three.dump());
  ExtendedRational n5d2p(10, 4, true);
  EXPECT_EQ("5/2+", n5d2p.dump());
}

}  // namespace
